<template>
  <div class="app-container">
    <!-- 输入表单 -->
    <el-form label-width="120px">
		
		
		<!-- 课程 -->
		<el-form-item label="课程列表">
		<el-select
		  v-model="testsList.courseId"
		  placeholder="请选择">
		  <el-option
		    v-for="course in CourseList"
		    :key="course.id"
		    :label="course.title"
		    :value="course.id"/>
		</el-select>
				 </el-form-item>
				 
		<!-- 课程讲师 -->
		<el-form-item label="课程讲师">
		<el-select
		  v-model="testsList.teacherId"
		  placeholder="请选择">
		  <el-option
		    v-for="teacher in teacherList"
		    :key="teacher.id"
		    :label="teacher.name"
		    :value="teacher.id"/>
		</el-select>
		 </el-form-item>
		 
      <el-form-item label="测试题目">
        <el-input v-model="testsList.text" />
      </el-form-item>
	  <el-form-item label="单题分数">
	    <el-input v-model="testsList.csfs" />
	  </el-form-item>
	  <el-form-item label="正确答案">
	    <el-input v-model="testsList.tf" />
	  </el-form-item>

      <el-form-item>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="saveOrUpdate()">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
	import courseApi from '@/api/course'
	import teacherApi from '@/api/teacher'
import testsApi from '@/api/test'

export default {
  data() {
    return {
     testsList: {},
	 teacherList:{},
	 CourseList:{},
      saveBtnDisabled: false // 保存按钮是否禁用，防止表单重复提交
    }
  },

  // 页面渲染成功
  created() {
    if (this.$route.params.id) {
      this.fetchDataById(this.$route.params.id)
    }
	// 获取讲师列表
	this.initTeacherList();
	this.initCourseList()
  },

  methods: {
	  //获取讲师列表
	        initCourseList() {
	    courseApi.list().then(response => {
	      this.CourseList = response.data.items
	    })
	  },
 //获取讲师列表
       initTeacherList() {
   teacherApi.list().then(response => {
     this.teacherList = response.data.items
   })
 },
    saveOrUpdate() {
      // 禁用保存按钮
      this.saveBtnDisabled = true
      if (!this.testsList.id) {
        this.saveData()
      } else {
        this.updateData()
      }
    },

    // 新增
    saveData() {
      // debugger
      testsApi.save(this.testsList).then(response => {
        this.$message.success(response.message)
        this.$router.push({ path: '/test/list' })
      })
    },

    // 根据id更新记录
    updateData() {
      // teacher数据的获取
      testsApi.updateById(this.testsList).then(response => {
        this.$message.success(response.message)
        this.$router.push({ path: '/test/list' })
      })
    },

    // 根据id查询记录
    fetchDataById(id) {
      testsApi.getByIds(id).then(response => {
        this.testsList = response.data.item
		console.log(this.testsList+"BB");
      })
    }
  }
}
</script>